<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-button v-if="isAuth('article:cat:save')" type="primary" @click="addOrUpdateHandle()">新增顶级分类</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" border style="width: 100%;">
      <el-table-column prop="id" header-align="center" align="center" width="80" label="ID"/>
      <table-tree-column prop="catName" header-align="center" label="分类名称"/>
      <el-table-column prop="sort" header-align="center" align="center" width="80" label="排序号"/>
      <el-table-column prop="isShow" header-align="center" align="center" label="是否显示">
        <template slot-scope="scope">
          <el-select @change="showChange(scope.row.id,scope.row.isShow)" v-model="scope.row.isShow">
            <el-option v-for="item in showOption" :label="item.name" :key="item.key" :value="item.key"/>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('article:cat:update')" type="text" size="small"
                     @click="addOrUpdateHandle(scope.row.menuId)">
            修改
          </el-button>
          <el-button v-if="isAuth('article:cat:delete')" type="text" size="small" @click="deleteHandle(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
  import TableTreeColumn from '@/components/table-tree-column'
  import AddOrUpdate from './cat-add-or-update'
  import {treeDataTranslate} from '@/utils'

  export default {
    data() {
      return {
        dataForm: {},
        list: [],
        dataListLoading: false,
        addOrUpdateVisible: false,
        showOption: [
          {name: "显示", key: 1},
          {name: "隐藏", key: 0}
        ]
      }
    },
    components: {
      TableTreeColumn,
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl('/article/cat/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.status === 200) {
            this.list = treeDataTranslate(data.list, 'id');
            this.dataListLoading = false
          } else {
            this.$message.error(data.message);
          }
        })
      },
      showChange(id, isShow) {
        this.$http({
          url: this.$http.adornUrl('/article/cat/setShow/'),
          method: 'post',
          data: {
            id: id,
            isShow: isShow
          }
        }).then(({data}) => {
          if (data && data.status === 200) {
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.message)
          }
        });
      },
      floorChange(id, isFloor) {
        this.$http({
          url: this.$http.adornUrl('/goods/cat/setFloor/'),
          method: 'post',
          data: {
            id: id,
            isFloor: isFloor
          }
        }).then(({data}) => {
          if (data && data.status === 200) {
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.message)
          }
        });
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(row) {
        this.$confirm(`确定删除分类名为 [${row.catName}] 吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/article/cat/delete/${row.id}`),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.status === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.message)
            }
          })
        });
      }
    }
  }
</script>
